package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("categoryController")
@RequestMapping("admin/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //新增菜品分类
    @PostMapping("")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增菜品或套餐分类:{}", categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    //分页查询菜品分类
    @GetMapping("/page")
    public Result getCategoryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询菜品或套餐分类:{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.getCategoryPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    //依据id删除分类
    @DeleteMapping("")
    public Result deleteCategory(@RequestParam String id) {
        log.info("依据id删除分类:{}",id);
        categoryService.deleteCategory(Integer.parseInt(id));
        return Result.success();
    }

    //依据分类类型进行查询,菜品或套餐
    @GetMapping("/list")
    public Result<List<Category>> getCategory(@RequestParam String type) {
        log.info("依据分类类型分类查询:{}", type);
        List<Category> list = categoryService.list(Integer.parseInt(type));
        return Result.success(list);
    }

    //启用/禁用该分类
    @PostMapping("/status/{status}")
    public Result updateCategoryStatus(@PathVariable("status")String status,String id) {
        log.info("禁用/启用分类:{}",status);
        categoryService.updateCategoryStatus(Integer.parseInt(status),Long.parseLong(id));
        return Result.success();
    }

    //修改该分类信息
    @PutMapping("")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类:{}", categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }



}
